from sqlalchemy.orm import Session
from . import models, schemas

def get_images_reply(db: Session, images_reply_id: int):
    return db.query(models.ImagesReply).filter(models.ImagesReply.id == images_reply_id).first()

def get_images_replies(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.ImagesReply).offset(skip).limit(limit).all()

def create_images_reply(db: Session, images_reply: schemas.ImagesReplyCreate):
    db_images_reply = models.ImagesReply(**images_reply.dict())
    db.add(db_images_reply)
    db.commit()
    db.refresh(db_images_reply)
    return db_images_reply

def update_images_reply(db: Session, images_reply_id: int, images_reply: schemas.ImagesReplyUpdate):
    db_images_reply = db.query(models.ImagesReply).filter(models.ImagesReply.id == images_reply_id).first()
    if db_images_reply:
        for var, value in vars(images_reply).items():
            setattr(db_images_reply, var, value) if value else None
        db.add(db_images_reply)
        db.commit()
        db.refresh(db_images_reply)
    return db_images_reply

def delete_images_reply(db: Session, images_reply_id: int):
    db_images_reply = db.query(models.ImagesReply).filter(models.ImagesReply.id == images_reply_id).first()
    if db_images_reply:
        db.delete(db_images_reply)
        db.commit()
    return db_images_reply